<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\adminapi\validate\marketing;

use think\Validate;

class StoreSeckillValidate extends Validate
{

    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'title' => 'require',
        'start_time' => 'require|integer',
        'end_time' => 'require|integer|gt:start_time',
        'status'    =>  'require|in:0,1',
        'num'   =>  'require|egt:1',
        'later_minute'  =>  'require|egt:1',
        'has_limit'    =>  'require|in:0,1',
        'products'  =>  'checkProductRelationsRules'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'title.require' => '30063',
        'start_time.require' => '30064',
        'start_time.integer' => '30065',
        'end_time.require' => '30066',
        'end_time.integer' => '30067',
        'end_time.gt' => '30068',
        'status.require' => '30069',
        'status.in' => '30070',
        'num.require' => '30071',
        'num.egt' => '30072',
        'later_minute.require' => '30073',
        'later_minute.egt' => '30074',
        'has_limit.require' => '30075',
        'has_limit.in' => '30076',
    ];

    protected $scene = [
        'save' => ['title', 'start_time', 'end_time', 'status', 'num', 'later_minute', 'has_limit'],
    ];

    protected function checkProductRelationsRules($value, $rule, $data)
    {
        if (!empty($value)) {
            foreach ($value as $product) {
                if (!isset($product['product_id']) || !isset($product['sort'])) {
                    return '30077'; // 商品规则错误
                }
            }
        }
        return true;
    }
}
